package com.txh.blog.config;

import com.txh.blog.filter.UserInfoStatuFilter;
import com.txh.blog.util.ResourceBundleUtils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean registrationBean(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(userInfoStatuFilter());
        filterRegistrationBean.setName("userLoginStatuFilter");
        /*filterRegistrationBean.addInitParameter("paramName", "paramValue");*/
        List<String>  urlPatterns = getUrlPatterns();
        filterRegistrationBean.setUrlPatterns(urlPatterns);
        filterRegistrationBean.addInitParameter("exclusions",getExUrlPatterns());
        return filterRegistrationBean;

    }

    private List<String> getUrlPatterns(){
        List<String>  urlPatterns = new ArrayList<>();
        ResourceBundle resourceBundle = ResourceBundleUtils.getResourceBundle("filterUrls");
        String [] urls= resourceBundle.getString("urlParrtens").split(",");
        for (int i = 0;i<urls.length;i++){
            urlPatterns.add(urls[i]);
        }
        return urlPatterns;
    }

    private String getExUrlPatterns(){
        return ResourceBundleUtils.getResourceBundle("filterUrls").getString("exUrlParrtens");
    }
    @Bean
    public UserInfoStatuFilter userInfoStatuFilter(){
        return new UserInfoStatuFilter();
    }
}
